package com.battle.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.battle.pojo.entity.Organization;
import com.battle.pojo.entity.UserOrganizationMp;
import com.battle.service.repository.in.OrganizationRepository;
import com.battle.service.repository.in.UserOrganizationMpRepository;
import com.battle.service.service.in.UserOrganizationMpService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class UserOrganizationMpServiceImpl implements UserOrganizationMpService {
    private final UserOrganizationMpRepository userOrganizationMpRepository;
    private final OrganizationRepository organizationRepository;

    @Override
    public List<Organization> getOrganizationListByUserId(Long userId) {
        UserOrganizationMp entity = new UserOrganizationMp();
        entity.setUserId(userId);
        entity.setIsDelete(false);
        List<UserOrganizationMp> userOrganizationList = userOrganizationMpRepository.getListByEntity(entity);
        if (CollectionUtil.isNotEmpty(userOrganizationList)) {
            List<Long> organizationIds = userOrganizationList.stream().map(UserOrganizationMp::getOrganizationId).collect(Collectors.toList());
            List<Organization> organizationList = organizationRepository.getListByIds(organizationIds);
            if (CollectionUtil.isNotEmpty(organizationList)) {
                return organizationList;
            }
        }
        return List.of();
    }
}
